using BasicScript.Component;
using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class OxygenMachineItem : BaseItem
	{
		private enum State
		{
			Close = 0,
			Open = 1
		}

		private const int MIN_NUM = 10;

		private const int MAX_NUM = 99;

		private const int MIN_ANGLE = -60;

		private const int MAX_ANGLE = 60;

		private State state;

		private Transform btn;

		private Transform numTrans;

		private Transform pointer1;

		private Transform pointer2;

		private int num;

		private float curBtnAngle;

		private float originalBtnAngle;

		private float targetBtnAngle;

		private float curPointer1Angle;

		private float curPointer2Angle;

		private float timer;

		private float numTimer;

		private float pointer1Timer;

		private float waitTimer1;

		private float addVal1;

		private float pointer2Timer;

		private float waitTimer2;

		private float addVal2;

		private bool isPlayAnim;

		private SpriteAssets spriteAssets;

		private SpriteRenderer[] numRenders;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btn = model.Find("Btn");
			numTrans = model.Find("Num");
			pointer1 = model.Find("Pointer1");
			pointer2 = model.Find("Pointer2");
			spriteAssets = model.GetComponent<SpriteAssets>();
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isPlayAnim)
			{
				timer += Time.deltaTime * 2f;
				curBtnAngle = Mathf.LerpAngle(originalBtnAngle, targetBtnAngle, timer);
				btn.SetLocalRotationZ(curBtnAngle);
				if (timer >= 1f)
				{
					isPlayAnim = false;
				}
			}
			if (state == State.Open)
			{
				numTimer += Time.deltaTime;
				if (numTimer > 0.5f)
				{
					numTimer = 0f;
					SetNumber(Mathf.Clamp(num + Random.Range(-1, 2), 10, 99), numTrans);
				}
				pointer1Timer += Time.deltaTime;
				if (pointer1Timer > waitTimer1)
				{
					pointer1Timer = 0f;
					RandomPointerParam(out waitTimer1, out addVal1, addVal1);
				}
				SetPointer1Angle(Mathf.Clamp(curPointer1Angle + addVal1, -60f, 60f));
				pointer2Timer += Time.deltaTime;
				if (pointer2Timer > waitTimer2)
				{
					pointer2Timer = 0f;
					RandomPointerParam(out waitTimer2, out addVal2, addVal2);
				}
				SetPointer2Angle(Mathf.Clamp(curPointer2Angle + addVal2, -60f, 60f));
			}
			else
			{
				if (curPointer1Angle != 0f)
				{
					SetPointer1Angle(Mathf.MoveTowardsAngle(curPointer1Angle, 0f, 1f));
				}
				if (curPointer2Angle != 0f)
				{
					SetPointer2Angle(Mathf.MoveTowardsAngle(curPointer2Angle, 0f, 1f));
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (!isPlayAnim)
			{
				if (state == State.Open)
				{
					originalBtnAngle = 0f;
					targetBtnAngle = 90f;
					SetState(State.Close);
				}
				else
				{
					originalBtnAngle = 90f;
					targetBtnAngle = 0f;
					SetState(State.Open);
				}
				timer = 0f;
				numTimer = 0f;
				pointer1Timer = 0f;
				pointer2Timer = 0f;
				isPlayAnim = true;
			}
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Open)
				{
					SetNumber(Random.Range(10, 99), numTrans);
					RandomPointerParam(out waitTimer1, out addVal1, addVal1);
					RandomPointerParam(out waitTimer2, out addVal2, addVal2);
					PlayDragEffect();
				}
				else if (state == State.Close)
				{
					SetNumber(0, numTrans);
					StopDragEffect();
				}
			}
		}

		private void RandomPointerParam(out float waitTimer, out float addVal, float originalAddVal)
		{
			waitTimer = Random.Range(0.8f, 1.5f);
			addVal = originalAddVal;
			if (addVal == 0f)
			{
				if (Random.Range(0, 2) == 0)
				{
					addVal = Random.Range(-1.2f, -0.7f);
				}
				else if (Random.Range(0, 2) == 0)
				{
					addVal = Random.Range(0.5f, 1f);
				}
			}
			else if (addVal < 0f)
			{
				addVal = Random.Range(0.5f, 1f);
			}
			else if (addVal > 0f)
			{
				addVal = Random.Range(-1.2f, -0.7f);
			}
		}

		private void SetPointer1Angle(float val)
		{
			curPointer1Angle = val;
			pointer1.SetLocalRotationZ(curPointer1Angle);
		}

		private void SetPointer2Angle(float val)
		{
			curPointer2Angle = val;
			pointer2.SetLocalRotationZ(curPointer2Angle);
		}

		public void SetNumber(int num, Transform numTrans)
		{
			this.num = num;
			string[] array = new string[2];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = num.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			if (numRenders == null)
			{
				numRenders = new SpriteRenderer[2];
				numRenders[0] = numTrans.Find("0").GetComponent<SpriteRenderer>();
				numRenders[1] = numTrans.Find("1").GetComponent<SpriteRenderer>();
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				numRenders[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
